﻿$(document).ready(function () {
    $("#logout").click(function () {
        $.post("/handler/user.ashx?action=logout", {}, function () {
            window.location.reload();
        }, "json");
    });

    $(".toTop").click(function () {
        $('html, body').animate({ scrollTop: '0px' }, 800);
    });
    $(".toBottom").click(function () {
        $('html, body').animate({ scrollTop: $('body').height() }, 800);
    });
    $(".toContent").click(master.suggest);

    master.getSuggestCount();
});

var master = {
    url: "/handler/suggest.ashx?action=",

    getSuggestCount: function () {
        $.get(master.url + "todoCount", {}, function (data) {
            if (data && data.count) {
                $("#suggest_todocount").html(data.count);
                if (data.count > 0) {
                    $("#suggest_todocount").css({ "color": "red", "cursor": "pointer" }).click(master.showTodoList);
                }
            }
        }, "json");
    },

    showTodoList: function () {
        $.get(master.url + "myHandlerList", {}, function (data) {
            master.suggestList = data.list;
            master.suggestFlag = 0;
            if (data && data.list) {
                var first = data.list[0];
                var html = [];
                html.push("<table class='niceTab' width='100%' cellspacing='0' cellpadding='0' border='0'>");
                html.push("<tr><th class='three'>建议主题</th><td id='ps_title'>" + first.Title + "</td></tr>");
                html.push("<tr><th>建议内容</th><td id='ps_content'>" + first.Description.replace(/\n/g, "<br />") + "<br />-----------------------<p><strong>" + first.ControlUserName + "(" + first.ControlUserId + ")</strong>：<br />" + first.ControlComment.replace(/\n/g, "<br />") + "</p></td></tr>");
                html.push("<tr><th>建议涉及部门</th><td id='ps_dept'>" + first.RelateDeptName + "</td></tr>");
                html.push("<tr><th>反馈</th><td><textarea class='input' id='feedback'>" + (first.Feedback ? first.Feedback : "") + "</textarea><br /><br />");
                html.push("<input type='button' class='small button' value='反馈进展' onclick='master.feedback()' />");
                if (data.list.length > 1) {
                    html.push("<input type='button' id='nextBtn' class='small button' value='暂不处理 / 下一条' onclick='master.next()' />");
                }
                html.push("</td></tr>");
                html.push("</table>");

                master.popU = master.popU || new PopU();
                master.popU.initPop({
                    type: 'string',
                    title: "待办事务处理",
                    contents: html.join(""),
                    w: 500,
                    y: $(window).scrollTop() + 60,
                    drag: true
                });
                master.popU.showPop();
            }
        }, "json");
    },

    feedback: function () {
        if (master.suggestFlag < 0 || master.suggestFlag >= master.suggestList.length)
            return false;

        var feedback = $.trim($("#feedback").val());
        if (feedback == "") {
            alert("反馈内容不能为空");
            $("#feedback").focus();
            return false;
        }

        $.post(master.url + "feedback", { id: master.suggestList[master.suggestFlag].Id, feedback: feedback }, function (data) {
            if (data && data.success) {
                if (master.suggestFlag == master.suggestList.length - 1) {
                    master.popU.close();
                    master.getSuggestCount();
                    return true;
                }
                master.next();
                return true;
            }
            if (data && data.error)
                alert(data.error);
            return false;
        }, "json");
    },

    next: function () {
        if (master.suggestList.length <= 1)
            return false;

        var current = master.suggestList[++master.suggestFlag];
        $("#ps_title").html(current.Title);
        $("#ps_description").html(current.Description.replace(/\n/g, "<br />") + "<br />-----------------------<p><strong>" + current.ControlUserName + "(" + current.ControlUserId + ")</strong>：<br />" + current.ControlComment.replace(/\n/g, "<br />"));
        $("#ps_dept").html(current.RelateDeptName);
        $("#feedback").val(current.Feedback);

        if (master.suggestFlag == master.suggestList.length - 1)
            $("#nextBtn").hide();
    },

    suggest: function () {
        var html = [];
        html.push("<table class='niceTab' width='100%' cellspacing='0' cellpadding='0' border='0'>");
        html.push("<tr><th class='three'>建议主题</th><td><input type='text' id='stitle' class='date input' /></td></tr>");
        html.push("<tr><th>建议内容</th><td><textarea class='input' id='scontent'></textarea></td></tr>");
        html.push("<tr><th>建议涉及部门</th><td><input type='text' id='sdept' class='date input' /></td></tr>");
        html.push("</table>");
        html.push("<label class='inline'><a href='javascript:void(0)' onclick='master.showMySuggest()'>查看我的建议列表</a></label>");
        html.push("<button class='button alert right' onclick='master.submitSuggest()'>提交建议</button>");

        master.popU = master.popU || new PopU();
        master.popU.initPop({
            type: 'string',
            title: "建议反馈",
            contents: html.join(""),
            w: 500,
            y: $(window).scrollTop() + 60,
            drag: true
        });
        master.popU.showPop();
    },

    showMySuggest: function () {
        window.location.href = "/Suggest.aspx";
    },

    submitSuggest: function () {
        var title = $('#stitle').val();
        var content = $('#scontent').val();
        var dept = $('#sdept').val();

        if ($.trim(title) == "") {
            alert("建议主题不能为空");
            $("#stitle").focus();
            return false;
        }
        if ($.trim(content) == "") {
            alert("建议内容不能为空");
            $("#scontent").focus();
            return false;
        }
        $.post(master.url + "suggest", { title: title, content: content, dept: dept }, function (data) {
            if (data.error) {
                alert(data.error);
                return;
            }

            alert("成功提交建议，感谢您的反馈");
            master.popU.close();
        }, "json");
    }
};